package com.hyr.springcloud.ticket.dao;

import com.hyr.springcloud.ticket.demain.Ticket;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface TicketRespository extends JpaRepository<Ticket,Integer> {
    List<Ticket> findByOwner(Integer owner);

    Ticket findByTicketNum(Integer ticketNum);

    Ticket findByTicketNumAndLockUser(Integer tickNum,Integer LockUser);

    @Override
    @Modifying(clearAutomatically = true)
    Ticket save(Ticket ticket);

    @Modifying
    @Query("UPDATE ticket set lockUser=?1 where lockUser is null and ticketNum=?2")
    int lockTicket(Integer userinfoId,Integer tickNum);

    @Modifying
    @Query("UPDATE ticket set owner=?1,lockUser = NULL where lockUser = ?1 and ticketNum=?2")
    int moveTicket(Integer userinfoId,Integer tickNum);

    @Modifying
    @Query("UPDATE ticket set lockUser = NULL where lockUser = ?1 and ticketNum=?2")
    int unLockTicket(Integer userinfoId, Integer tickNum);

    @Modifying
    @Query("UPDATE ticket set owner=NULL where owner = ?1 and ticketNum=?2")
    int unMoveTicket(Integer userinfoId,Integer tickNum);
}
